home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / MACVOGL- / EXAMPLES / CURVES.C < prev    next >
C/C++ Source or Header  |  1992-07-19  |  3KB  |  190 lines

  1. #include <stdio.h>
  2.  
  3. #ifdef SGI
  4. #include "gl.h"
  5. #include "device.h"
  6. #else
  7. #include "vogl.h"
  8. #include "vodevice.h"
  9. #endif
  10.  
  11. /*
  12.  * curve basis types
  13.  */
  14. Matrix    bezier = {
  15.     {-1.0,    3.0,    -3.0,    1.0},
  16.     {3.0,    -6.0,    3.0,    0.0},
  17.     {-3.0,    3.0,    0.0,    0.0},
  18.     {1.0,    0.0,    0.0,    0.0} 
  19. };
  20.  
  21. Matrix    cardinal = {
  22.     {-0.5,    1.5,    -1.5,    0.5},
  23.     {1.0,    -2.5,    2.0,    -0.5},
  24.     {-0.5,    0.0,    0.5,    0.0},
  25.     {0.0,    1.0,    0.0,    0.0}
  26. };
  27.  
  28. Matrix    bspline = {
  29.     {-1.0 / 6.0,    3.0 / 6.0,    -3.0 / 6.0,    1.0 / 6.0},
  30.     {3.0 / 6.0,    -6.0 / 6.0,    3.0 / 6.0,    0.0},
  31.     {-3.0 / 6.0,    0.0,        3.0 / 6.0,    0.0},
  32.     {1.0 / 6.0,    4.0 / 6.0,    1.0 / 6.0,    0.0}    
  33. };
  34.  
  35. /*
  36.  *     Geometry matrix to demonstrate basic spline segments
  37.  */
  38. float   geom1[4][3] = {
  39.     { -180.0, 10.0, 0.0 },
  40.     { -100.0, 110.0, 0.0 },
  41.     { -100.0, -90.0, 0.0 },
  42.     { 0.0, 50.0, 0.0 }
  43. };
  44.  
  45. /*
  46.  *     Geometry matrix to demonstrate overlapping control points to
  47.  *    produce continuous (Well, except for the bezier ones) curves
  48.  *    from spline segments
  49.  */
  50. float    geom2[6][3] = {
  51.     { 200.0, 480.0, 0.0 },
  52.     { 380.0, 180.0, 0.0 },
  53.     { 250.0, 430.0, 0.0 },
  54.     { 100.0, 130.0, 0.0 },
  55.     { 50.0,  280.0, 0.0 },
  56.     { 150.0, 380.0, 0.0 }
  57. };
  58.  
  59. /*
  60.  * using curves
  61.  */
  62. main()
  63. {
  64.     char    dev[20];
  65.     int    i;
  66.     short    val;
  67.  
  68.     winopen("curves");
  69.  
  70.     qdevice(KEYBD);
  71.     unqdevice(INPUTCHANGE);
  72.  
  73.     ortho2(-200.0, 400.0, -100.0, 500.0);
  74.  
  75.     color(BLACK);
  76.     clear();
  77.  
  78.     color(YELLOW);
  79.  
  80.     /*
  81.      * label the control points in geom1
  82.      */
  83.         for (i = 0; i < 4; i++) {
  84.         cmov2(geom1[i][0], geom1[i][1]);
  85.         sprintf(dev, "%d", i);
  86.         charstr(dev);
  87.     }
  88.                                  
  89.     /*
  90.      * label the control points in geom2
  91.      */
  92.     for (i = 0; i < 6; i++) {
  93.         cmov2(geom2[i][0], geom2[i][1]);
  94.         sprintf(dev, "%d", i);
  95.         charstr(dev);
  96.     }
  97.  
  98.     /*
  99.      * set the number of line segments appearing in each curve to 20
  100.      */
  101.     curveprecision((short)20);
  102.  
  103.     /*
  104.      * copy the bezier basis matrix into the basis matrix stack and
  105.      * set the curve basis accordingly.
  106.      */
  107.     defbasis((short)1, bezier);
  108.     curvebasis((short)1);
  109.  
  110.     color(RED);
  111.  
  112.     /*
  113.      * draw a curve using the current basis matrix (bezier in this case)
  114.      * and the control points in geom1
  115.      */
  116.     crv(geom1);
  117.  
  118.     cmov2(70.0, 60.0);
  119.     charstr("Bezier Curve Segment");
  120.  
  121.     cmov2(-190.0, 450.0);
  122.     charstr("Three overlapping Bezier Curves");
  123.  
  124.     /*
  125.      * crvn draws overlapping curve segments according to geom2, the
  126.      * number of curve segments drawn is three less than the number of
  127.      * points passed, assuming there are a least four points in the
  128.      * geometry matrix (in this case geom2). This call will draw 3
  129.      * overlapping curve segments in the current basis matrix - still
  130.      * bezier.
  131.      */
  132.     crvn(6L, geom2);
  133.  
  134.     qread(&val);
  135.  
  136.     /*
  137.      * load in the cardinal basis matrix
  138.      */
  139.     defbasis((short)1, cardinal);
  140.     curvebasis((short)1);
  141.  
  142.     color(MAGENTA);
  143.  
  144.     cmov2(70.0, 10.0);
  145.     charstr("Cardinal Curve Segment");
  146.  
  147.     /*
  148.      * plot out a curve segment using the cardinal basis matrix
  149.      */
  150.     crv(geom1);
  151.  
  152.     cmov2(-190.0, 400.0);
  153.     charstr("Three overlapping Cardinal Curves");
  154.  
  155.     /*
  156.      * now draw a bunch of them again.
  157.      */
  158.     crvn(6L, geom2);
  159.  
  160.     qread(&val);
  161.  
  162.     /*
  163.      * change the basis matrix again
  164.      */
  165.     defbasis((short)1, bspline);
  166.     curvebasis((short)1);
  167.  
  168.     color(GREEN);
  169.  
  170.     cmov2(70.0, -40.0);
  171.     charstr("Bspline Curve Segment");
  172.  
  173.     /*
  174.      * now draw our curve segment in the new basis...
  175.      */
  176.     crv(geom1);
  177.  
  178.     cmov2(-190.0, 350.0);
  179.     charstr("Three overlapping Bspline Curves");
  180.  
  181.     /*
  182.      * ...and do some overlapping ones
  183.      */
  184.     crvn(6L, geom2);
  185.  
  186.     qread(&val);
  187.  
  188.     gexit();
  189. }
  190.